﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace EasyStudy.tools
{
    public class DBAutoBackup
    {
        public static readonly string BACKUP_FOLDER_NAME = "db_backup";

        private static string createFolder()
        { 
            // 创建文件夹
            string exeDir = System.AppDomain.CurrentDomain.BaseDirectory;
            string backupDir = Configuration.GetInstance().BackupDir;

            if (backupDir.Trim() == "") {
                backupDir = exeDir;
            }

            DateTime now = DateTime.Now;

            string dirName = now.Year.ToString() + "-" + now.Month.ToString();
            string dirFullName = backupDir + BACKUP_FOLDER_NAME + @"/" + dirName;

            if (!Directory.Exists(dirName))
            {
                Directory.CreateDirectory(dirFullName);
            }

            return dirFullName;
        }


        public static void StartBackup()
        {
            string dirFullName = createFolder();

            string exeDir = System.AppDomain.CurrentDomain.BaseDirectory;
            DateTime now = DateTime.Now;

            string dirName = now.Year.ToString() + "-" + now.Month.ToString();
            string fileName = "/EbbDataBase_" + now.Year.ToString() + "_" + 
                now.Month.ToString() + "_" + now.Day.ToString() + ".db";

            //if (!File.Exists(fileName))
            //{
                File.Copy(exeDir + @"/EbbDataBase.db", dirFullName + fileName, true);
            //}
        }
    }
}
